/** 
*    twExtJS - ToscaWidgets wrapper for ExtJS (see http://extjs.com)
*    
*    Distributed under the terms of Lesser GNU Public License.
*    
*    (c) 2007 - Ricardo Girardi Sixel 
*/

Ext.grid.NestedGridRowExpander = function(config){
    Ext.apply(this, config);
    Ext.grid.NestedGridRowExpander.superclass.constructor.call(this);
    
};

Ext.extend(Ext.grid.NestedGridRowExpander ,Ext.grid.PanelRowExpander, {

    buildTabPanel : function(record, body) {
    
        var items = this.items;
        
        // Recreate grid stores 
        for(var i = 0; i < items.length; i++) {
        	var tab = items[i];
        	for(var j = 0; j < tab.items.length; j++) {
        		var grid = tab.items[j];
        		if(grid.basestore ==undefined){
	   				grid.basestore = grid.ds;
	   			}
	   			grid.ds = this.getNewStore(grid.basestore,grid.basestore.id+record.id);
        	}
        	
        }

  	    var panel = Ext.grid.NestedGridRowExpander.superclass.buildTabPanel.call(this,record, body);
  	    
        var items = this.items;
          
        for(var i = 0; i < panel.items.length; i++) {
         	var items = panel.items.items[i]
         	if(items.items!=undefined){
	   			for(var j=0;j<items.items.length;j++){
	   				var grid = items.items.items[j];
	   				grid.id = grid.id+record.id;

	   				grid.masterRecord = record;

	   				if(grid.store.baseNewRecordUrl == undefined){
	   					grid.store.baseNewRecordUrl = grid.store.newRecordUrl;
	   				} 
	   				
	   				this.loadDetails(record,grid); 
	   				
	   				var addButton = grid.topToolbar[1];
	   				
	   				addButton.handler = this.handler;
	   				addButton.grid = grid;
	   			}
   			}
        }
        		            
        return panel;        
    },
    
    getNewStore : function(store,id){
		    return new Ext.data.Store({
		    	id : id,
			    autoLoad : false,
				proxy: new Ext.data.HttpProxy({url: store.proxy.conn.url}),
				reader : new Ext.data.JsonReader({
					            root: store.reader.meta.root,
					            totalProperty: store.reader.meta.totalProperty 
	      		        	},store.reader.recordType),	
				remoteSort: false,
				newRecordUrl : store.newRecordUrl,
				handleAdd : function(grid ){
						    cb = {store: this,grid : grid, success: this.handleSuccess, failure: this.handleFailure}
						    Ext.lib.Ajax.request('GET', this.newRecordUrl, cb);
					},
					
				handleSuccess : function(response, options){
					 var encResponse = Ext.util.JSON.encode(response.responseText);
					 var jsonData = Ext.util.JSON.decode(encResponse);
					 jsonData = Ext.util.JSON.decode(jsonData);
					 var r = new Ext.data.Record(jsonData.result[0]);
					 this.grid.stopEditing();
					 this.store.add(r);
					 var p = this.store.indexOf(r);
					 this.grid.startEditing(p, 0);
				},
				
				handleFailure : function(response, options){
				}
			})
    },
    
    handler : function(button){
    	var grid=button.grid;
    	
    	grid.stopEditing();
    	
    	grid.store.newRecordUrl = grid.store.baseNewRecordUrl+'/'+grid.masterRecord.data.id;
		grid.store.handleAdd(grid);
    }
    
});    